<?php 

/**
 * 计算两数相加之和
 * @param [type] $a
 * @param [type] $b
 * @return void
 */
 function add($a, $b)
{
    $sum = $a + $b;
    return $sum;
}
$a = '1';
$b = 2;
$c = add($a, $b);
echo "$a + $b = $c" . PHP_EOL;

$n5 = '学院君';
$n6 = 2;
$s3 = add($n5, $n6);
echo "$n5 + $n6 = $s3" . PHP_EOL;
echo '<br/>';

function add2(int $a,int $b) : int
{
    $sum = $a + $b;
    return $sum;
}
echo "\n";

$n5 = 8.0;
$n6 = 9.1;
$s3 = add2($n5, $n6);
echo "$n5 + $n6 = $s3" . PHP_EOL;
echo "\n";

// $n5 = '学院君';
// $n6 = 2;
// $s3 = add2($n5, $n6);
// echo "$n5 + $n6 = $s3" . PHP_EOL;

echo "<br/> 值传递 <br/>";
$m = 1;
$n = 2;
$c = add($m, $n);
printf("\$m = %d\n", $m);
printf("\$c = %d\n", $c);

echo "<br/> 引用传递 <br/>";
function add3 (int &$a,int $b) {
    $a += $b;
}
add3($m, $n);
printf("\$m = %d\n", $m);

echo "<br/> 数组函数 <br/>";
$arr = [1, 3, 8, 7, 6];
sort($arr);
print_r($arr);
rsort($arr);
print_r($arr);
printf("数组最大值：%d\n", max($arr));
printf("数组最小值：%d\n", min($arr));

echo "<br/> 数学函数 <br/>";
$num = 100;
$n1 = pow(100, 2);
$n2 = sqrt(100);
$n3 = decbin(100);
$n4 = mt_rand(0, 100);
echo $n1,"\n", $n2,"\n", $n3,"\n", $n4;

echo "<br/> 文件系统函数 <br/>";
file_put_contents('test1.txt', '你好，学院君'); //快速写入内容到文件 test.txt(不存在则自动创建)
$content = file_get_contents('test1.txt'); //从文件 test.txt 中读取内容
var_dump($content);

$file = fopen('test2.txt', 'w'); //写入模式
fwrite($file, '你好，'); 
fwrite($file, '学院君！'); //继续写入
fclose($file); //关闭这个文件句柄

$file = fopen('test2.txt', 'r');
$content = '';
while(!feof($file)){
    $content .= fread($file, 1024);
}
fclose($file);
var_dump($content);

unlink('test1.txt');
unlink('test2.txt');